﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZHDJ.Data;
using ZHDJ.WinService.Job;

namespace WinServiceTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=AppBox;User ID=sa;Password=123456;");
            SyncData();
        }

        public void SyncData()
        {
            //SqlServerHelper.ConnectionKey = "AppExamV5";
            var tempSql = @" insert UserInfo (UserCode,UserName,UserPwd,DeptID,Worker,RacingFen,UserMember) 
                                        values('{0}','{1}','A115A58E6C40DA1887F9',13,'考生',-1,1); ";
            var dt = GetNotSyncUser().Tables[0];
            var sql = new StringBuilder();
            foreach (DataRow dr in dt.Rows)
            {
                sql.AppendFormat(tempSql, dr["UserNo"], dr["UserName"]);
            }
            SqlServerHelper.ExecuteNonQuery(sql.ToString());
        }

        public DataSet GetNotSyncUser()
        {
            var sql = string.Format(@"select * from tblpartyorgmember where userno not in ({0})", GetExamUserIds());
            return MySqlHelper.ExecuteDataSet(sql);
        }

        private string GetExamUserIds()
        {
            var dt = GetExamUser().Tables[0];
            if (dt.Rows.Count == 0)
            {
                return string.Empty;
            }
            var texts = new List<string>();
            foreach (DataRow dataRow in dt.Rows)
            {
                if (dataRow["UserCode"].ToString() != "")
                {
                    texts.Add(dataRow["UserCode"].ToString());
                }
            }
            return "'" + (texts.Count < 1 ? "" : string.Join("','", texts.ToArray())) + "'";
        }
        private DataSet GetExamUser()
        {
            var sql = @"select * from UserInfo";
            return SqlServerHelper.ExecuteDataSet(sql);
        }
    }
}
